<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class SymbolModel extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("entities/symbolentity");
        $this->load->model("entities/symboldataentity");
        $this->load->model("entities/cmpdataentity");
    }

    public function getSymbol()
    {
        $query = $this->db->query('SELECT a.id, a.url_val FROM tbl_symbol a ORDER BY a.url_val ASC');
        return $query->result('SymbolEntity');
    }

    public function getSymbolDetails($symbol)
    {
        $this->db->select('id, url_val');
        $this->db->where(array('url_val' => $symbol), 1, 0);
        $query = $this->db->get('tbl_symbol');
        $rows = $query->result('SymbolEntity');
        if (isset($rows[0])) {
            return $rows[0];
        }
        return null;
    }

    public function getArraySymbolDetails($aSymbol)
    {
        $this->db->select('id, url_val');
        $query = $this->db->where_not_in('url_val', $aSymbol);
        $result = $query->get('tbl_symbol')->result('SymbolEntity');
        return $result;
    }

    public function getSymbolData($startDate, $endDate, $cmp)
    {
        $this->db->select('id, symbol_id, date, close_price')->where('date >=', $startDate->
            format('Y-m-d'))->where('date <=', $endDate->format('Y-m-d'))->where('symbol_id',
            $cmp)->order_by('date', 'desc');
        $query = $this->db->get('tbl_company_details');

        return $query->result('SymbolDataEntity');
    }

    public function insertSymbols($arrSymbol)
    {
        $this->db->trans_begin();
        foreach ($arrSymbol as &$item) {
            $item = (array )$item;
        }
        $this->db->insert_batch('tbl_symbol', $arrSymbol);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function insertCompanyDetailsForCron($arrDetails, $fDate, $tDate)
    {
        foreach ($arrDetails as &$sd) {
            $sd = (array )$sd;
        }
        $this->db->trans_begin();
        $this->db->insert_batch('tbl_company_details', $arrDetails);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function insert1CompanyDetailsForCron($arrDetails, $fDate, $tDate)
    {
        $this->db->reconnect();
        $this->db->trans_begin();
        $this->db->insert('tbl_company_details', $arrDetails);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function insertCompanyDetailsForInstall($arrDetails, $fDate, $tDate)
    {
        $arrWhere = array(
            'date >=' => $fDate->format('Y-m-d'),
            'date <=' => $tDate->format('Y-m-d'),
            'symbol_id' => $arrDetails[0]->getSymbolId());
        $this->db->trans_begin();
        $this->db->delete('tbl_company_details', $arrWhere);
        foreach ($arrDetails as &$value) {
            $value = (array )$value;
        }
        $this->db->insert_batch('tbl_company_details', $arrDetails);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return 0;
        } else {
            $this->db->trans_commit();
        }
        return 1;
    }

    public function getSymbolDataForNew()
    {
        $query = $this->db->query('SELECT a.id, a.url_val FROM tbl_symbol a WHERE a.id NOT IN (SELECT b.symbol_id FROM tbl_company_details b) ORDER BY a.url_val ASC');
        return $query->result('SymbolEntity');
    }

    public function deletesymbol($id)
    {
        $this->db->trans_begin();
        try {
            $this->db->where('symbol_id_b', $id)->or_where('symbol_id_a', $id)->delete('tbl_watchlist_details');
            $this->db->delete('tbl_company_details', array('symbol_id' => $id));
            $this->db->delete('tbl_symbol', array('id' => $id));
            if ($this->db->trans_status() === false) {
                $this->db->trans_rollback();
                return 0;
            } else {
                $this->db->trans_commit();
                return 1;
            }
        }
        catch (exception $e) {
            $this->db->trans_rollback();
            return 0;
        }
        return 1;
    }
}
?>